<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    :title="title"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="类别名称" prop="title" style="width: 100%">
            <el-input v-model="form.title" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="显示图标"
            prop="tb"
            :rules="[{ required: true, message: '请选择显示图标', trigger: 'change' }]"
          >
            <el-popover
              placement="bottom-start"
              width="450"
              trigger="click"
            >
              <el-button slot="reference" size="small" style="padding: 0px;">
                <el-image
                  v-if="form.tb"
                  :src="baseURL+form.tb.substring(1,form.tb.length)"
                  style="height: 32px;width: 32px;"
                />
                <i v-else style="font-size: 20px;height: 32px;width: 32px;line-height: 32px" class="el-icon-plus" />
              </el-button>
              <abbreviation-image-select ref="iconSelect" v-model="form.tb" />
            </el-popover>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="优先级" prop="px" style="width: 100%">
            <el-input-number v-model="form.px" :min="1" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="字体颜色"
            prop="fontColor"
            :rules="[{ required: true, message: '请选择字体颜色', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.fontColor" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="背景色A"
            prop="bakA"
            :rules="[{ required: true, message: '请选择开始色', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.bakA" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="背景色B"
            prop="bakB"
            :rules="[{ required: true, message: '请选择结束色', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.bakB" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="预览">
            <div
              :style="{ width: '120px',height: '60px',boxShadow: '0 2px 7px 0px '+ form.bakA,
                        background: 'linear-gradient(0deg,'+form.bakA+','+form.bakB+')'}"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="zt">
            <el-radio-group v-model="form.zt">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="bz">
            <el-input
              v-model="form.bz"
              type="textarea"
              maxlength="100"
              show-word-limit
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveEntity, updateEntity } from '@/api/fwfl/index'
import AbbreviationImageSelect from '@/components/AbbreviationImageSelect'
export default {
  name: 'FwflEntity',
  components: { AbbreviationImageSelect },
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      title: '新增',
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      form: {
        zt: true,
        bz: '',
        title: '',
        px: 1,
        bakA: '#ffffff',
        bakB: '#ffffff',
        fontColor: '#ffffff',
        tb: ''
      },
      rules: {
        title: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
        px: [{ required: true, message: '排序值不能为空', trigger: 'blur' }],
        zt: [{ required: true, message: '请选分类状态', trigger: 'change' }]
      },
      status: [{ id: 0, value: false, label: '否' },
        { id: 1, value: true, label: '是' }]
    }
  },
  methods: {
    openEntity(data) {
      if (undefined === data || data === null || data.id === undefined || data.id === null) {
        this.title = '新增'
        this.dialogFormVisible = true
      } else {
        this.title = '编辑'
        this.dialogFormVisible = true
      }
      this.form = Object.assign({}, data)
    },
    save(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.form.id !== null && undefined !== this.form.id) {
            updateEntity(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.$message.error('更新失败！')
              this.loading = false
            })
          } else {
            saveEntity(this.form).then(res => {
              this.$message.success('保存成功！')
              this.loading = false
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    close() {
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.dialogFormVisible = false
      this.$emit('fetch-data', this.form)
    }
  }
}
</script>

<style scoped>

</style>
